package tue.cse.advanceddatabases.project;

import java.util.logging.Logger;

import tue.cse.advanceddatabases.project.db.JPAResourceBean;
import tue.cse.advanceddatabases.project.db.User;
import tue.cse.advanceddatabases.project.db.UserManager;


public class LoginManager {
	private final Logger logger = Logger.getLogger(LoginManager.class.getName());
	
    protected JPAResourceBean jpaResourceBean;
	private boolean signedIn = false;
	
	private Boolean isAdmin = false;
	private User _user = new User();
	private UserManager _userManager;
	
	private UserManager getUserManager() {
		if (_userManager == null)
			_userManager = new UserManager(jpaResourceBean);
		return _userManager;
	}
	
        public void setIsAdmin(boolean value){
	    	this.isAdmin = value;
	}
	public boolean getIsAdmin(){
	        //TODO: return this.isAdmin;
		if (_user == null)
			return false;
		
		logger.info("System Role is:" + _user.getSystemRole());
		if ("U".equalsIgnoreCase(_user.getSystemRole()))
	    	return false;
		
		return true;
	}
		
	public String loginAction() {
		String action = null;

		UserManager um = getUserManager();
		_user = um.findUserByLoginID(_user.getLoginId());

		if (_user == null)
			return "fail";
		
		if (!_user.getPassword().equalsIgnoreCase(_user.getPassword()))
			return "fail";
		
		signedIn = true;
		
		//Goran: added different menu for Administrator and SimpleUser users   
		if (_user.getSystemRole().equalsIgnoreCase("A"))
		  setIsAdmin(true);
		else
		  setIsAdmin(false);
		
		return "main";
	}
	
	public String logoutAction(){
		signedIn = false;
		_user = new User();
		return "logout";
	}

	public String registerAction(){
		System.out.println("Registering user");
		
		UserManager um = getUserManager();
		
		try{
			
			um.add(_user);
			
		}catch (Exception e){
			e.printStackTrace();
			logger.info("Error on registration:" + e.getMessage());
			return "fail";
		}
		
		signedIn = true;
		return "surccess";
	}
	
    public void setJpaResourceBean(JPAResourceBean jpaResourceBean) {
        this.jpaResourceBean = jpaResourceBean;
    }

    public JPAResourceBean getJpaResourceBean() {
        return jpaResourceBean;
    }

    /** User Properties **/
    
	public String getLoginId() {
		return _user.getLoginId();
	}
	
	public void setLoginId(String id) {
		_user.setLoginId(id);
	}
	
	public String getPassword() {
		return _user.getPassword();
	}

	public void setPassword(String pwd) {
		_user.setPassword(pwd);
	}

	//Goran: add properties for SystemRole
	public String getSystemRole() {
		return _user.getSystemRole();
	}

	public void setSystemRole(String systemRole) {
		_user.setSystemRole(systemRole);
	}
	
	public String getFirstName() {
		return _user.getFirstName();
	}

	public void setFirstName(String firstName) {
		_user.setFirstName(firstName);
	}
	
	public String getLastName() {
		return _user.getLastName();
	}

	public void setLastName(String lastName) {
		_user.setLastName(lastName);
	}
	
	public String getCity() {
		return _user.getCity();
	}

	public void setCity(String city) {
		_user.setCity(city);
	}

	public String getCountry() {
		return _user.getCountry();
	}

	public void setCountry(String country) {
		_user.setCountry(country);
	}

	public String getAddress() {
		return _user.getAddress();
	}

	public void setAddress(String address) {
		_user.setAddress(address);
	}

	public String getZipCode() {
		return _user.getZipCode();
	}

	public void setZipCode(String zipCode) {
		_user.setZipCode(zipCode);
	}
    
	public String getEmail() {
		return _user.getEmail();
	}

	public void setEmail(String email) {
		_user.setEmail(email);
	}
    
}
